﻿/*
 * Created by SharpDevelop.
 * User: Jack Brinegar
 * Date: 1/28/2013
 * Time: 4:10 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

namespace AdminDesktop.Controls
{
	/// <summary>
	/// Description of ViewPerson.
	/// </summary>
	public partial class ucViewPerson : UserControl
	{
		bool isDirty = false;
		bool isNewPatroller = false;
		ButternutSkiPatrol.Person _patroller;
		ucRoster _owner;
			
		public bool NewPatroller
		{
			get {return isNewPatroller;}
			set {isNewPatroller = value;}
		}
		
		public ucViewPerson()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			this.Dock = DockStyle.Fill;
			
			
			DataGridViewComboBoxColumn col = (DataGridViewComboBoxColumn)this.dgrdServiceDays.Columns[1];
			col.Items.Clear();
			
			for(int i = 0; i < DataEngine.APP_CONFIG.ServiceTypes.ServiceTypeList.Count;i++)
			{
				col.Items.Add(DataEngine.APP_CONFIG.ServiceTypes.ServiceTypeList[i].Name);
				//this.dgrdServiceDays.Columns[1]
			}
		}
		
		public void LoadData(ButternutSkiPatrol.Person patroller)
		{
			//Patroller information
			_patroller=patroller;
			this.txtFirstName.Text = Utility.TitleCaseString(_patroller.FirstName);
			this.txtLastName.Text = Utility.TitleCaseString(_patroller.LastName);
			this.txtMiddleName.Text=Utility.TitleCaseString(_patroller.MiddleName);
			this.txtNspNumber.Text = _patroller.NspNumber;
			this.txtSpouse.Text = Utility.TitleCaseString(_patroller.Spouse);
			this.txtAddress.Text = Utility.TitleCaseString(_patroller.Address);
			this.txtCity.Text = Utility.TitleCaseString(this._patroller.City);
			this.txtState.Text = this._patroller.State.ToUpper();
			this.txtZipCode.Text = _patroller.ZipCode;
			this.txtHomePhone.Text = _patroller.HomePhone;
			this.txtEmailAddr.Text = _patroller.Email1;
			this.cboLevel.Text=_patroller.Level;
			
			//ServiceDays information
			this.dgrdServiceDays.Rows.Clear();
			
			if(_patroller.ServiceDays!=null)
			{
				this.gboxServiceDates.Text=this.gboxServiceDates.Tag.ToString() + " (" + _patroller.ServiceDays.Count + ")";
				if(_patroller.ServiceDays.Count>0){this.dgrdServiceDays.RowCount=_patroller.ServiceDays.Count+1;}
				dgrdServiceDays.ClearSelection();
				for(int i = 0; i < _patroller.ServiceDays.Count;i++)
				{
					this.dgrdServiceDays.Rows[i].Cells[0].Value=_patroller.ServiceDays[i].Date;
					this.dgrdServiceDays.Rows[i].Cells[1].Value=_patroller.ServiceDays[i].ServiceType;
				}
				
			}
			
			this.Refresh();
			isDirty=false;
		}
		
		void BtnSaveClick(object sender, EventArgs e)
		{
			//populate the person object
			_patroller.FirstName=this.txtFirstName.Text;
			_patroller.MiddleName=this.txtMiddleName.Text;
			_patroller.LastName=this.txtLastName.Text;
			_patroller.NspNumber=this.txtNspNumber.Text;
			_patroller.Spouse=this.txtSpouse.Text;
			_patroller.Email1=this.txtEmailAddr.Text;
			_patroller.HomePhone=this.txtHomePhone.Text;
			_patroller.Address=this.txtAddress.Text;
			_patroller.City=this.txtCity.Text;
			_patroller.State=this.txtState.Text;
			_patroller.ZipCode=this.txtZipCode.Text;
			_patroller.Level=this.cboLevel.Text;
			
			//save the service day information
			_patroller.ServiceDays = new System.Collections.Generic.List<ButternutSkiPatrol.ServiceDay>();
			foreach(DataGridViewRow row in this.dgrdServiceDays.Rows)
			{
				if(row.IsNewRow==false)
				{
					ButternutSkiPatrol.ServiceDay day = new ButternutSkiPatrol.ServiceDay();
					day.Date=row.Cells[0].Value.ToString();
					day.ServiceType=row.Cells[1].Value.ToString();
					_patroller.ServiceDays.Add(day);
				}
			}
			
			if(isNewPatroller)
			{
				//add the person object to the roster
				DataEngine.AddPatroller(_patroller);
				isNewPatroller=false;
			}
			else
			{
				DataEngine.SaveRosterFile();
			}
			this.Visible=false;
			
			ucRoster owner = (ucRoster)this.Parent.Parent.Parent;
			owner.LoadData();
			
			MainForm mainForm = (MainForm)this.Parent.Parent.Parent.Parent.Parent.Parent;
			mainForm.ForceRosterRefresh();

						
		}
		
		private void setFullName()
		{
			string mi = string.Empty;
				
			if(this.txtMiddleName.Text.Trim().Length>0)
			{
				mi = this.txtMiddleName.Text.Substring(0,1).ToUpper() + ". ";
			}
			
			this.lblFullName.Text = this.txtFirstName.Text + " " + mi + this.txtLastName.Text;
		}
		
		private void setDirty()
		{
			isDirty = true;
		}
		
		void TxtFirstNameTextChanged(object sender, EventArgs e)
		{
			setDirty();
			if(this.txtFirstName.Text.Trim().Length>0)
			{
				setFullName();
			}
		}
		
		void TxtMiddleNameTextChanged(object sender, EventArgs e)
		{
			setDirty();
			if(this.txtMiddleName.Text.Trim().Length>0)
			{
				setFullName();
			}
		}
		
		void TxtLastNameTextChanged(object sender, EventArgs e)
		{
			setDirty();
			if(this.txtLastName.Text.Trim().Length>0)
			{
				setFullName();
			}
		}
		
		void ViewPerson_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
		{
			int count = this.dgrdServiceDays.Rows.Count-1;
			this.gboxServiceDates.Text=this.gboxServiceDates.Tag.ToString() + " (" + count.ToString() + ")";
		}
		
		void ViewPerson_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
		{
			int count = this.dgrdServiceDays.Rows.Count-1;
			this.gboxServiceDates.Text=this.gboxServiceDates.Tag.ToString() + " (" + count.ToString() + ")";
		}
		
		void BtnDeleteClick(object sender, EventArgs e)
		{
			DialogResult result1 = MessageBox.Show("Are you sure you want to remove " + this.lblFullName.Text + " from them roster?",Application.ProductName,MessageBoxButtons.YesNo,MessageBoxIcon.Question);
			if(result1==DialogResult.Yes)
			{
				DataEngine.dataFile.roster.Patrollers.Remove(_patroller);
			}
			DataEngine.SaveRosterFile();
			
			ucRoster owner = (ucRoster)this.Parent.Parent.Parent;
			owner.LoadData();
			
			MainForm mainForm = (MainForm)this.Parent.Parent.Parent.Parent.Parent.Parent;
			mainForm.ForceRosterRefresh();
			
			foreach(Control ctrl in this.Controls)
			{
				System.Type chkType = ctrl.GetType();
					switch(chkType.ToString())
				{
					case "System.Windows.Forms.TextBox":
					case "System.Windows.Forms.ComboBox":
						ctrl.Text = "";
						break;
				}
				
				this.dgrdServiceDays.Rows.Clear();
			}
		}
		
		void UcViewPersonLoad(object sender, EventArgs e)
		{
			
		}
	}
}
